#include "SplineOffset.h"
#include "BoundaryBuilder.h"
#include "BSpline.h"
#include "dmEntityColl.h"
#include "GeoConvert.h"

int CSplineOffset::InsertEntity(dmDbLayer *layer,CdmEntityBase *entity,double offset,dmDbEntity *&entityout)
{
	dmDPoint *pts=0;
	dmPolyData *poly=dmPolyData::New();
	dmDbPolyline* polyline=dmDbPolyline::New();
	CdmEntityBase* lpdmEntityBase=0;
	
	BEGIN_VALIDATE
	VALIDATE(CDataConvertTool::ConvertToPolyline(polyline,entity));
	VALIDATE(CBaseOffset::OffsertCurve(polyline,offset,lpdmEntityBase));
	lpdmEntityBase->ToPolyData(poly);
	VALIDATE(dmPolyData2dmDPoint(poly,pts));
	VALIDATE(MakeSpline(layer,pts,poly->GetNumberOfPoints(),entityout));

	EXIT_VALIDATE
	polyline->Delete();
	poly->Delete();
	SAFE_DELETE_ARRAY(pts);
	RETURN_VALIDATE_RESULT
}

int CSplineOffset::MakeSpline(dmDbLayer* layer,dmDPoint *pts,int npts,CdmEntityBase*& entityout)
{
	CBSpline* spline=new CBSpline();
	BEGIN_VALIDATE
	VALIDATE(spline->Make(npts,pts));
	dmDbSpline* pSplineBottom  = dmDbSpline::New();
	layer->InsertEntity(pSplineBottom);
	VALIDATE(pSplineBottom->SetGraphData(3,spline->GetControlNum(),spline->GetControlPoints(),0,0,spline->GetControlNum() + 4,spline->GetKnots(),0,1));
	entityout = pSplineBottom;
	res=1;
	EXIT_VALIDATE
	if(spline) delete spline;
	if(entityout) entityout->Display();
	RETURN_VALIDATE_RESULT
}
int CSplineOffset::CallBack(dmDbLayer* layer,dmDPoint* pts,int npts,CdmEntityBase*& entity)
{
	return MakeSpline(layer,pts,npts,entity);
}